<template>
  <div class="shop-cart" @click.stop="showList">
    <div class="content">
      <div class="content-left">
        <div class="logo-wrapper">
          <div class="logo" :class="{ highlight: totalCount > 0 }">
            <i
              class="icon-shopping_cart"
              :class="{ highlight: totalCount > 0 }"
            ></i>
          </div>
          <div class="num" v-show="totalCount > 0">{{ totalCount }}</div>
        </div>

        <div class="price" :class="{ highlight: totalCount > 0 }">
          ￥{{ totalPrice }}
        </div>
        <div class="desc">另需配送费￥{{ deliveryPrice }}元</div>
      </div>
      <div class="content-right">
        <div
          class="pay"
          :class="totalPrice < minPrice ? 'not-enough' : 'enough'"
        >
          {{ remainPrice }}
        </div>
      </div>
    </div>

    <!-- 购物列表 -->
    <transition name="fold">
      <div class="shopcart">
        <!-- 遮罩层 -->
        <div
          class="shadow-layer"
          v-show="showCartList"
          @click.stop="hideCartList"
        ></div>
        <div class="shopcart-list" v-show="showCartList">
          <div class="list-header" v-show="selectFoods.length > 0">
            <h1 class="title">购物车</h1>
            <span class="empty" @click.stop="clearFoods">清空</span>
          </div>
          <div class="list-content">
            <ul>
              <li
                class="food"
                v-for="(food, index) in selectFoods"
                :key="index"
              >
                <span class="name">{{ food.name }}</span>
                <div class="price">
                  <span>￥{{ food.price }}</span>
                </div>
                <div class="cartcontrol-wrapper">
                  <cart-control :food="food"></cart-control>
                </div>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
import cartcontrol from "@/components/cart-control/cart-control";
export default {
  props: {
    selectFoods: {
      type: Array,
    },
    deliveryPrice: {
      type: Number,
      default: 0,
    },
    minPrice: {
      type: Number,
      default: 0,
    },
  },
  data: function () {
    return {
      showCartList: false,
    };
  },
  components: {
    "cart-control": cartcontrol,
  },
  computed: {
    totalCount: function () {
      let total = 0;
      this.selectFoods.forEach((item) => {
        total += item.count;
      });

      return total;
    },
    totalPrice: function () {
      let total = 0;
      this.selectFoods.forEach((item) => {
        total += item.count * item.price;
      });
      return total;
    },
    remainPrice: function () {
      if (this.totalPrice == 0) {
        return `￥${this.minPrice}元起送`;
      } else if (this.totalPrice < this.minPrice) {
        return `还差${this.minPrice - this.totalPrice}元起送`;
      } else {
        return "去结算";
      }
    },
  },
  methods: {
    showList() {
      this.showCartList = true;
    },
    hideCartList() {
      this.showCartList = false;
    },
    clearFoods(){
      this.selectFoods = (function(){
        return []
      })()
    }
  },
};
</script>

<style lang="less">
.shop-cart {
  position: fixed;
  left: 0;
  bottom: 0;
  z-index: 100;
  width: 100%;
  height: 48px;
  .content {
    display: flex;
    background: rgba(7, 17, 27, 1);
    color: #999;
    .content-left {
      flex: 1;
      .logo-wrapper {
        vertical-align: middle;
        display: inline-block;
        position: relative;
        top: -10px;
        margin: 0 12px;
        padding: 6px;
        width: 56px;
        height: 56px;
        border-radius: 50%;
        background: #141d27;
        box-sizing: border-box;
        .logo {
          width: 100%;
          height: 100%;
          background: #333;
          border-radius: 50%;
          text-align: center;
          .icon-shopping_cart {
            line-height: 44px;
            font-size: 24px;
            color: #80858a;
          }
          .icon-shopping_cart.highlight {
            color: #fff;
          }
        }
        .num {
          position: absolute;
          top: 0;
          right: 0;
          width: 24px;
          height: 16px;
          line-height: 16px;
          text-align: center;
          border-radius: 16px;
          font-size: 10px;
          font-weight: bold;
          color: #fff;
          background: #f01414;
        }
        .logo.highlight {
          background: #00a0dc;
        }
      }
      .price {
        display: inline-block;
        line-height: 48px;
        border-right: 1px solid rgba(255, 255, 255, 0.1);
        font-size: 16px;
        padding-right: 12px;
        font-weight: bold;
        margin-right: 12px;
      }
      .price.highlight {
        color: #fff;
      }
      .desc {
        display: inline-block;
        line-height: 48px;
        font-size: 10px;
      }
    }
    .content-right {
      flex: 0 0 105px;
      width: 105px;
      .pay {
        height: 48px;
        line-height: 48px;
        text-align: center;
        font-size: 12px;
        font-weight: bold;
      }
      .pay.enough {
        background: #00d43c;
        color: #fff;
      }
      .pay.not-enough {
        background: #2b333b;
      }
    }
  }

  .shopcart-list {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    z-index: -1;
    transform: translate3d(0, -100%, 0);
    .list-header {
      display: flex;
      justify-content: space-between;
      padding: 0 18px;
      height: 40px;
      line-height: 40px;
      align-items: center;
      border-bottom: 1px solid rgba(7, 17, 27, 0.2);
      background: #f3f5f7;
      .title {
        font-size: 14px;
        color: rgba(7, 17, 27);
      }
      .empty {
        font-size: 12px;
        color: #00a0dc;
      }
    }
    .list-content {
      padding: 0 18px;
      max-height: 217px;
      overflow: hidden;
      background: #fff;
      .food {
        position: relative;
        padding: 12px 0;
        box-sizing: border-box;
        .name {
          line-height: 24px;
          font-size: 14px;
          color: rgb(7, 17, 27);
        }
        .price {
          position: absolute;
          right: 90px;
          bottom: 12px;
          line-height: 24px;
          font-size: 14px;
          font-weight: 700;
          color: rgb(240, 20, 20);
        }
        .cartcontrol-wrapper {
          position: absolute;
          display: inline-block;
          right: 0;
          bottom: 6px;
        }
      }
    }
  }
  .shopcart-list.fold-enter-active,
  .shopcart-list.fold-leave-active {
    transition: all 0.5s;
  }
  .shopcart-list.fold-enter,
  .shopcart-list.fold-leave-to {
    transform: translate3d(0, 0, 0);
  }
  .shopcart {
    height: 100%;
    width: 100%;
    .shadow-layer {
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background: rgba(0, 0, 0, 0.8);
      z-index: -1;
    }
  }
}
</style>